import axios from 'axios'
import { Message } from 'element-ui'
const requests = axios.create({
  // 配置对象
  // 基础路径
  baseURL: '/api',
  timeout: 50000
})

requests.interceptors.request.use((config) => {
  // Access-Control-Allow-Origin
  config.headers['Access-Control-Allow-Origin'] = '*'
  if (localStorage.getItem('token')) {
    config.headers.token = localStorage.getItem('token')
  }
  return config
},
err => {
  return Promise.reject(err)
}
)

requests.interceptors.response.use((res) => {
  // Access-Control-Allow-Origin
  res.headers['Access-Control-Allow-Origin'] = '*'
  if (res.data === '') {
    localStorage.removeItem('token')
  } else if (res.data.code !== 200) {
    Message.error(res.data.data)
  }
  return res.data
}, (error) => {
  return Promise.reject(error)
})

export default requests
